//
//  SentenceAnswerInputCell.swift
//  Game
//
//  Created by woniu on 2025/9/12.
//

import Foundation
import UIKit
import SnapKit

class SentenceAnswerInputCell: UITableViewCell {
	static let reuseIdentifier = "SentenceAnswerInputCell"

    private let cardView = UIView()
	private let indexLabel = UILabel()
	private let topicField = UITextField()
	private let answerField = UITextField()
	private var sentenceItem: SentenceItem?

	override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
		super.init(style: style, reuseIdentifier: reuseIdentifier)
		setup()
	}
	required init?(coder: NSCoder) { super.init(coder: coder); setup() }

	private func setup() {
		selectionStyle = .none
		backgroundColor = .white
        cardView.backgroundColor = .white
        cardView.addShadow()
        contentView.addSubview(cardView)
        
		indexLabel.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
		indexLabel.textColor = .white
        indexLabel.textAlignment = .center
        indexLabel.backgroundColor = Colors.blue
        indexLabel.circular(15)
        cardView.addSubview(indexLabel)

        topicField.textColor = .black
        topicField.circular(20)
        topicField.borderSet(Colors.line2, 1)
		topicField.placeholder = "题目"
		topicField.delegate = self
        topicField.clearButtonMode = .whileEditing
        topicField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 40))
        topicField.leftViewMode = .always
        cardView.addSubview(topicField)

        answerField.textColor = .black
		answerField.placeholder = "答案"
        answerField.circular(20)
        answerField.borderSet(Colors.line2, 1)
		answerField.delegate = self
        answerField.clearButtonMode = .whileEditing
        answerField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 40))
        answerField.leftViewMode = .always
        cardView.addSubview(answerField)
        
        cardView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }

		indexLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(30)
			make.width.equalTo(30)
            make.centerY.equalToSuperview()
		}

		topicField.snp.makeConstraints { make in
			make.left.equalTo(indexLabel.snp.right).offset(8)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(40)
		}

		answerField.snp.makeConstraints { make in
			make.left.equalTo(topicField.snp.left)
			make.right.equalTo(topicField.snp.right)
			make.top.equalTo(topicField.snp.bottom).offset(10)
			make.height.equalTo(40)
		}
	}

    public func updateUI(item:SentenceItem,index:Int) {
        self.sentenceItem = item
		indexLabel.text = "\(index)"
		topicField.text = item.topic
		answerField.text = item.answer
	}
}

extension SentenceAnswerInputCell: UITextFieldDelegate {
	func textFieldDidEndEditing(_ textField: UITextField) {
		guard let it = sentenceItem else { return }
		if textField == topicField {
			it.topic = textField.text ?? ""
		} else if textField == answerField {
			it.answer = textField.text ?? ""
		}
	}

	func textFieldShouldReturn(_ textField: UITextField) -> Bool {
		textField.resignFirstResponder()
		return true
	}
}

